﻿using Define;
using LogicCenter.Logic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tools.Base;
using Tools.EventObjectMoudle;

namespace LogicCenter.Threads
{
    public class MainDispatcherThread : SingletonTemplate<MainDispatcherThread>, IMyThread
    {
        private BackgroundWorker _worker = null;

        public void SetMyWorker(BackgroundWorker worker)
        {
            _worker = worker;
        }

        DateTime tLast_500MS = DateTime.Now;

        public void ThreadFunc(object sender, EventArgs e)
        {
            while (Program.NeedExitServer == false)
            {
                DateTime tNow = DateTime.Now;
                if((tNow - tLast_500MS).TotalMilliseconds > 500)
                {
                    tLast_500MS = tNow;

                    GlobalEventSource.getInstance().fireEvent((int)EventTypes.GameRunning);
                }

                PlayerManager.Instance().Tick();
                Thread.Sleep(1);
            }
        }

        public void WaitForExit()
        {
            MyConsole.WriteLineEx(string.Format("{0}正在退出", this.ToString()), ConsoleColor.Green);
            while (_worker != null && _worker.IsBusy)
            {
                Thread.Sleep(1);
            }
            MyConsole.WriteLineEx(string.Format("{0}退出完毕", this.ToString()), ConsoleColor.Green);
        }
    }
}
